package ar.edu.sovietrussia.customcomponents.login;

import java.util.Map;

import org.apache.wicket.authroles.authentication.panel.SignInPanel;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import services.user.PersonService;
import users.LibrarianRole;
import users.Person;
import ar.edu.sovietrussia.MyAuthenticatedWebSession;
import ar.edu.sovietrussia.customcomponents.maincomponentviews.MainLibrarianComponent;
import ar.edu.sovietrussia.customcomponents.maincomponentviews.MainUserComponent;
import ar.edu.sovietrussia.customcomponents.util.IDoReplaces;
import ar.edu.sovietrussia.customcomponents.util.IFeedbacker;
import ar.edu.sovietrussia.customcomponents.util.WrapperPanel;

/**
 */
public abstract class LoginPanel extends Panel implements IFeedbacker, IDoReplaces {

    private static final long serialVersionUID = 1L;

    private Person loggedPerson;

    private MyAuthenticatedWebSession authSession;

    @SpringBean
    private PersonService personService;

    public LoginPanel(final String id) {
        super(id);
        this.authSession = (MyAuthenticatedWebSession) this.getSession();

        this.loggedPerson = (Person) this.authSession.getAttribute("model");
        this.addComponents();
    }

    private void addComponents() {
        String id = "panel";
        Panel result;
        if (this.authSession.isSignedIn() & this.loggedPerson != null) {

            if (this.loggedPerson.getUserAccount().getRole().equals(LibrarianRole.getInstance())) {
                result = new MainLibrarianComponent(id) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public FeedbackPanel getFeedbackTarget() {
                        return LoginPanel.this.getFeedbackTarget();
                    }

                    @Override
                    public Map<String, WrapperPanel> getReplaceableComponents() {
                        return LoginPanel.this.getReplaceableComponents();
                    }

                };
            } else {
                result = new MainUserComponent(id);
            }
        } else {
            result = new SignInPanel(id) {

                private static final long serialVersionUID = 7432050165690156332L;

                @Override
                protected void onSignInSucceeded() {
                    Person model = LoginPanel.this.getPersonService().getPersonByUsername(this.getUsername());

                    this.getSession().setAttribute("model", model);

                    this.replaceWithRespectingMainComponent(model);
                }

                private void replaceWithRespectingMainComponent(final Person aModel) {
                    if (aModel.getUserAccount().getRole().equals(LibrarianRole.getInstance())) {
                        this.replaceWith(new MainLibrarianComponent(this.getId()) {

                            private static final long serialVersionUID = 1L;

                            @Override
                            public FeedbackPanel getFeedbackTarget() {
                                return LoginPanel.this.getFeedbackTarget();
                            }

                            @Override
                            public Map<String, WrapperPanel> getReplaceableComponents() {
                                return LoginPanel.this.getReplaceableComponents();
                            }

                        });
                    } else {
                        this.replaceWith(new MainUserComponent(this.getId()));
                    }
                }

                @Override
                protected void onSignInFailed() {
                    this.replaceWith(new ErrorInLoggingComponent(this.getId(), this));
                }

            };
        }
        this.add(result);
    }

    public void setLoggedPerson(final Person loggedPerson) {
        this.loggedPerson = loggedPerson;
    }

    public Person getLoggedPerson() {
        return this.loggedPerson;
    }

    public void setAuthSession(final MyAuthenticatedWebSession authSession) {
        this.authSession = authSession;
    }

    public MyAuthenticatedWebSession getAuthSession() {
        return this.authSession;
    }

    public void setPersonService(final PersonService personService) {
        this.personService = personService;
    }

    public PersonService getPersonService() {
        return this.personService;
    }

}
